package com.xyoto.community.task;

import cn.hutool.core.date.DateTime;
import com.xyoto.community.entity.main.HotDynamic;
import com.xyoto.community.enums.DynamicEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.comment.DynamicCommentMapper;
import com.xyoto.community.mapper.love.DynamicLoveMapper;
import com.xyoto.community.mapper.main.DynamicForwardMapper;
import com.xyoto.community.mapper.main.HotDynamicMapper;
import com.xyoto.community.po.DynamicHotNumPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实现热门动态，定时更新
 */
@Component
@Slf4j
public class HotDynamicTask {

    @Autowired
    private HotDynamicMapper hotDynamicMapper;

    @Autowired
    private DynamicLoveMapper dynamicLoveMapper;

    @Autowired
    private DynamicForwardMapper dynamicForwardMapper;

    @Autowired
    private DynamicCommentMapper dynamicCommentMapper;

    @Autowired
    private HotDynamicRedisImpl hotDynamicRedis;

    @Scheduled(cron ="0 0 1/2 * * ?")
    private void timingUpdateHotDynamic(){

        log.info("开始执行更新热门动态功能");
        List<DynamicHotNumPO> list = hotDynamicMapper.getAllHotDynamic();

        List<DynamicHotNumPO> comments = dynamicCommentMapper.getHotDynamicCommentCount(list);

        List<DynamicHotNumPO> forwards = dynamicForwardMapper.getHotDynamicForwardCount(list);

        List<DynamicHotNumPO> loves = dynamicLoveMapper.getHotDynamicLoveCount(list);

        Map<Integer, DynamicHotNumPO> map = new HashMap<>();
        //通过相同的DynamicId进行相互匹配
        for (DynamicHotNumPO dynamicHotNumPO : list) {
            map.put(dynamicHotNumPO.getDynamicId(), dynamicHotNumPO);
        }
        for (DynamicHotNumPO comment : comments) {
            Integer dynamicId = comment.getDynamicId();
            DynamicHotNumPO dynamicHotNumPO = map.get(dynamicId);
            if (dynamicHotNumPO == null) continue;
            if (StringUtils.isEmpty(comment.getHotCount())){
                comment.setHotCount(dynamicHotNumPO.getHotCount());
            }
            map.put(comment.getDynamicId(), comment);
        }
        for (DynamicHotNumPO forward : forwards) {
            Integer dynamicId = forward.getDynamicId();
            DynamicHotNumPO dynamicHotNumPO = map.get(dynamicId);
            if (dynamicHotNumPO == null) continue;
            if (StringUtils.isEmpty(forward.getDynamicCommentCount())){
                forward.setDynamicCommentCount(dynamicHotNumPO.getDynamicCommentCount());
            }
            if (StringUtils.isEmpty(forward.getHotCount())){
                forward.setHotCount(dynamicHotNumPO.getHotCount());
            }
            map.put(dynamicId,forward);
        }
        for (DynamicHotNumPO love : loves) {
            Integer dynamicId = love.getDynamicId();
            DynamicHotNumPO dynamicHotNumPO = map.get(dynamicId);
            if (dynamicHotNumPO == null) continue;
            if (StringUtils.isEmpty(love.getDynamicCommentCount())){
                love.setDynamicCommentCount(dynamicHotNumPO.getDynamicCommentCount());
            }
            if (StringUtils.isEmpty(love.getDynamicForwardCount())){
                love.setDynamicForwardCount(dynamicHotNumPO.getDynamicForwardCount());
            }
            if (StringUtils.isEmpty(love.getHotCount())){
                love.setHotCount(dynamicHotNumPO.getHotCount());
            }
            map.put(dynamicId,love);
        }
        //开始对热度值进行更新操作，将评论总数，转发总数，点赞总数进行添加
        List<HotDynamic> hotDynamics = new ArrayList<>();
        for (DynamicHotNumPO dynamicHotNumPO : list) {
            DynamicHotNumPO dynamicHotNumPO1 = map.get(dynamicHotNumPO.getDynamicId());
            HotDynamic hotDynamic = new HotDynamic();
            Integer commentCount = dynamicHotNumPO1.getDynamicCommentCount();
            Integer forwardCount = dynamicHotNumPO1.getDynamicForwardCount();
            Integer loveCount = dynamicHotNumPO1.getDynamicLoveCount();
            commentCount = commentCount == null ? 0:commentCount;
            forwardCount = forwardCount == null ? 0:forwardCount;
            loveCount = loveCount == null ? 0:loveCount;
            int currentHot = commentCount + forwardCount - dynamicHotNumPO.getHotCount();
            int hotCount = commentCount * 3 + forwardCount * 3 + loveCount * 4;
            currentHot = Math.max(currentHot, 0);
            hotDynamic.setHotCount(hotCount);
            hotDynamic.setCurrentHot(currentHot);
            hotDynamic.setDynamicId(dynamicHotNumPO1.getDynamicId());
            hotDynamic.setModifiedTime(new DateTime());

            hotDynamics.add(hotDynamic);
        }

        log.info("开始执行热门动态更新SQL");
        Integer updateHotDynamic = hotDynamicMapper.updateHotDynamic(hotDynamics);
        if (updateHotDynamic == null || updateHotDynamic ==0){
            ExceptionUtils.error(DynamicEnum.HOT_DYNAMIC_UPDATE_FAIL);
        }

        log.info("开始执行存入redis");
        hotDynamicRedis.saveHotDynamic(hotDynamics);

    }

}
